package com.cxm.qyyz.utils;

import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FormatUtil {
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat format = new SimpleDateFormat("MM.dd");

    /**
     * 格式化小数点后两位
     * date 2021/12/31
     * author yzx
     */
    public static String getStringByRound(String value, int roundCount) {
        return new BigDecimal(value).setScale(roundCount, BigDecimal.ROUND_HALF_UP).toString();
    }

    /**
     * 转换成整数
     */
    public static String toInt(Object value) {
        String valueStr = String.valueOf(value);
        return new BigDecimal(valueStr).setScale(0, BigDecimal.ROUND_UP).toString();
    }

    /**
     * 有小数点保留decimal位,无小数点转换成整数
     */
    public static String toRoundUp(Object value,int decimal) {
        String valueStr = String.valueOf(value);
        return new BigDecimal(valueStr).setScale(decimal, BigDecimal.ROUND_UP).stripTrailingZeros().toPlainString();
    }

    /**
     * 有小数点保留两位,无小数点转换成整数
     */
    public static String toRoundUp(Object value) {
        String valueStr = String.valueOf(value);
        return new BigDecimal(valueStr).setScale(2, BigDecimal.ROUND_UP).stripTrailingZeros().toPlainString();
    }

    /**
     * 整数和小数点分离,0为整数,1为小数(两位),2为拼接完整结果
     */
    public static String[] toMoney(Object value) {
        return toMoney(value,false);
    }

    /**
     * 整数和小数点分离,0为整数,1为小数(两位),2为拼接完整结果
     */
    public static String[] toMoney(Object value,boolean isWan) {
        if (value != null) {
            String valueStr = String.valueOf(value);
//            if (BaseUtil.isDigit(valueStr)) {
//                if (isWan && valueStr.split("\\.")[0].length() > 4){
//                    BigDecimal divide = new BigDecimal(valueStr).divide(new BigDecimal(10000),2,RoundingMode.DOWN);
//                    String[] split = divide.toString().split("\\.");
//                    String tempIntStr = split[0];
//                    String tempDecimalStr = split[1];
//                    return new String[]{tempIntStr,tempDecimalStr, String.format("%s.%s万", tempIntStr, tempDecimalStr)};
//                }
//                String roundValue = new BigDecimal(valueStr).setScale(2, RoundingMode.DOWN).toPlainString();
//                String[] split = roundValue.split("\\.");
//                return new String[]{split[0],split[1], String.format("%s.%s", split[0], split[1])};
//            }
        }
        return new String[]{"00","00","00.00"};
    }

    /**
     * 格式化小数点后两位 不四舍五入
     * date 2021/12/31
     * author yzx
     */
    public static String getStringByRoundDown(String value) {
        return new BigDecimal(value).setScale(2, BigDecimal.ROUND_DOWN).toString();
    }

    public static SpannableString getCrossText(String text) {
        SpannableString spannedText = new SpannableString(text);
        spannedText.setSpan(new StrikethroughSpan(), 0, spannedText.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannedText;
    }

    public static String getPositiveHalf(String amountText) {
        if (!TextUtils.isEmpty(amountText)) {
            if (amountText.contains(".")) {
                return amountText.substring(0, amountText.indexOf("."));
            }
        }
        return amountText;
    }

    public static String getNegativeHalf(String amountText) {
        if (!TextUtils.isEmpty(amountText)) {
            if (amountText.contains(".")) {
                return amountText.substring(amountText.indexOf("."));
            }
        }
        return "";
    }

    public static String phoneNumber(String phonenum) {
        //截取手机号码 方法一
        if (!TextUtils.isEmpty(phonenum) && phonenum.length() > 6) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < phonenum.length(); i++) {
                char c = phonenum.charAt(i);
                if (i >= 3 && i <= 6) {
                    sb.append('*');
                } else {
                    sb.append(c);
                }
            }

            return sb.toString();
        }
        return phonenum;
    }

    public static String getDecoratedTime(String original) {
        try {
            Date date = sdf.parse(original);
            if (date != null) {
                return sdf.format(date);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getYMD() {
        return sdf.format(new Date());
    }


    public static SpannableStringBuilder getEmphasizedText(String emphasizedText, String normalText) {
        SpannableStringBuilder builder = new SpannableStringBuilder();
        builder.append(emphasizedText);
        builder.setSpan(new StyleSpan(Typeface.BOLD), 0, builder.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        builder.append(normalText);
        return builder;
    }
}
